package com.sloan.community.common;

import com.sloan.community.exception.CommunityException;
import com.sloan.community.po.Article;
import com.sloan.community.po.Comment;
import com.sloan.community.po.Tag;
import com.sloan.community.po.User;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public interface Constant {

    String ERROR_MSG = "msg";
    String COMMENT_DEL_MSG = "已删除!";
    String NOTICE_DEL_MSG = "通知消息已删除!";
    Integer REPLY_COMMENT_LENGTH = 15;
    Integer NOTICE_MSG_LEN=50;

    String IMG_TEXT="[图片]";
    String IMG_TAG="<img[^>]*>";

    Long PARENT_ROOT_ID = 0L;

    List<String> STYLES = Arrays.asList("is-primary", "is-info", "is-success", "is-warning", "is-danger", "is-link", "");

    String SESSION_USER = "user";


    interface NoticeUrlPattern {
        String USER_ID = "userId";
        String PATTERN = "/ws/notice/{" + USER_ID + "}";
    }

    @Getter
    @AllArgsConstructor
    enum WeightFactor {
        LIKE_COUNT("点赞", 5d),
        COMMENT("评论", 2d),
        VIW("浏览量权重", 1d),
        SHARE("分享权重", 3d),
        DECAY_RATE("时间影响因子", 0.002d);
        private String name;
        private Double weight;
    }


    @Getter
    @AllArgsConstructor
    enum NoticeStatus {
        UN_READ(false, "未读",0),
        READ(true, "已读",1),
        ;
        private Boolean flag;
        private String msg;
        private Integer code;
    }

    @Getter
    @AllArgsConstructor
    enum NoticeType {
        ARTICLE(0, "文章通知"),
        COMMENT(1, "评论通知"),
        ;
        private Integer code;
        private String msg;

        public static NoticeType getByCode(Integer code) {
            for (NoticeType type : values()) {
                if (type.getCode().equals(code)) {
                    return type;
                }
            }
            return null;
        }
    }


    @Getter
    @AllArgsConstructor
    enum CommentStatus {
        HIDE(false, "HIDE"),
        SHOW(true, "SHOW"),
        ;
        private Boolean code;
        private String msg;
    }

    @Getter
    @AllArgsConstructor
    enum Oauth2 {

        GITEE(0, "Gitee"),
        GITHUB(1, "Github"),
        UNKNOWN(99, "未知");
        private Integer code;
        private String msg;

        public static Oauth2 getByCode(Integer code) {
            for (Oauth2 value : Oauth2.values()) {
                if (value.code.equals(code)) {
                    return value;
                }
            }
            return UNKNOWN;
        }
    }

    @Getter
    @AllArgsConstructor
    enum Sex {

        FEMALE(0, "女"),
        MALE(1, "男"),
        UNKNOWN(3, "不清楚"),
        ;
        private Integer code;
        private String msg;

    }

    static <T> Mono<Article> articleNotFoundException(T msg) {
        return Mono.error(CommunityException.of(HttpStatus.NOT_FOUND, "文章不存在" + msg));
    }

    static <T> Mono<Comment> commentNotFoundException(T msg) {
        return Mono.error(CommunityException.of(HttpStatus.NOT_FOUND, "评论不存在" + msg));
    }

    static <T> Mono<User> userNotFoundException(T msg) {
        return Mono.error(CommunityException.of(HttpStatus.NOT_FOUND, "用户不存在" + msg));
    }

    static <T> Mono<Tag> tagNotFoundException(T msg) {
        return Mono.error(CommunityException.of(HttpStatus.NOT_FOUND, "标签不存在" + msg));
    }
}
